/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.webapp.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import vn.webapp.model.Classes;
import vn.webapp.model.CourseTeacherClass;
import vn.webapp.model.CourseTeacherViewing;
import vn.webapp.model.Timetabling;
import vn.webapp.model.Timetablingraw;

@Repository("viewingDAO")
@SuppressWarnings({"unchecked", "rawtypes"})
@Transactional
public class ViewingDAOImpl extends BaseDao implements ViewingDAO {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public List<CourseTeacherClass> listViewing() {
        try {
            begin();
            Criteria criteria = getSession().createCriteria(CourseTeacherClass.class, "viewing");
            criteria.createAlias("viewing.classes", "classes");
            criteria.createAlias("viewing.coursingTimeTabling", "coursingTimeTabling");
            
            List<CourseTeacherClass> timetables = criteria.list();
            commit();
            return timetables;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return null;
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    public List<CourseTeacherViewing> list(){
        try {
            begin();
            Criteria criteria = getSession().createCriteria(CourseTeacherViewing.class, "courseteachers");
            List<CourseTeacherViewing> courseTeacher = criteria.list();
            commit();
            return courseTeacher;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return null;
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    public List<CourseTeacherClass> getByClassId(int classId) {
        try {
            begin();
            Criteria criteria = getSession().createCriteria(CourseTeacherClass.class, "viewing");
//            criteria.createAlias("viewing.classes", "classes");
//            criteria.createAlias("viewing.coursingTimeTabling", "coursingTimeTabling");
            criteria.add(Restrictions.eq("class_id", classId));
            List<CourseTeacherClass> timetables = criteria.list();
            commit();
            return timetables;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return null;
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    public List<Timetablingraw> getTimetablingByCourseTeacherId(int courseTeacherId){
        try {
            begin();
            Criteria criteria = getSession().createCriteria(Timetablingraw.class);
            criteria.add(Restrictions.eq("courseteacher_id", courseTeacherId));
            List<Timetablingraw> timetabling = criteria.list();
            commit();
            return timetabling;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return null;
        } finally {
            flush();
            close();
        }
    }
}
